<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/3/28
 * Time: 上午9:03
 */

namespace app\common\controller;


use app\common\model\Agent;
use app\common\model\CardMember;
use app\common\model\Merchant;
use app\common\model\System;
use app\common\model\WxOpen;
use think\Controller;


class Wxoption extends Controller
{
    public function wxoption($type,$param){
        switch($type){
            case "mid":
               $params=$param;
                break;
            case "top_mid":
                $params=$param;
                break;
            case "gh_id":
                $params=$param;
                break;
            case "card_id":
                $params=CardMember::where('card_id',$param)->value('mid');
                break;
        }
        $map['id']=$params;
        $WxOpen=new WxOpen();
        $Agent=new Agent();
        $Merchant=new Merchant();
        $merchantinfo=$Merchant->where($map)->field('wx_open_id,agent')->find();
        $mab['id'] =$merchantinfo['wx_open_id'];
        $config =$Agent->where(array('id' => $merchantinfo['agent']))->find();
        $users = $WxOpen->where($mab)->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;


    }
    //微信API参数 根据mid获取
    public function option($mid){

        $System=new System();
        $WxOpen=new WxOpen();
        $Agent=new Agent();
        $Merchant=new Merchant();
        $merchantinfo=$Merchant->where('id',$mid)->field('wx_open_id,agent')->find();
        $mab['id'] =$merchantinfo['wx_open_id'];
//        $config =$System->where(array('id' => 1))->find();
        $config =$Agent->where(array('id' => $merchantinfo['agent']))->find();
        $users = $WxOpen->where($mab)->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }

    //微信API参数  根据card_id获取
    public function options($card_id){

        $CardMember = new CardMember();
        $Merchant = new Merchant();
        $System=new System();
        $Agent=new Agent();
        $WxOpen=new WxOpen();
        $map['id'] = $CardMember->where('card_id',$card_id)->value('mid');
        $merchantinfo=$Merchant->where($map)->field('wx_open_id,agent')->find();
        $mab['id'] = $merchantinfo['wx_open_id'];
//        $config =$System->where(array('id' => 1))->field('component_token,component_encodingaeskey,component_appid,component_appsecret,open_ticket')->find();
        $config =$Agent->where(array('id' => $merchantinfo['agent']))->find();

        $users = $WxOpen->where($mab)->field('authorizer_appid,authorizer_refresh_token')->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }

    //获取所属商户
    public function get_topmid_mid($mid){
        $Merchant = new Merchant();
        $top_mid = $Merchant->where(['id'=>$mid])->value('top_mid');
        return $top_mid;
    }

    //获取代理商域名()
    public function getagenturl_mid($mid){
//        $CardMember = new CardMember();
        $Merchant = new Merchant();
        $Agent=new Agent();
//        $mid = $CardMember->where('card_id', $card_id)->value('mid');
        $agent_id = $Merchant->where(['id'=>$mid])->value('agent');
        $agent_url = $Agent->where(['id'=>$agent_id])->value('open_url');
        return 'https://'.$agent_url;
    }

}